<?php

namespace app\api\controller;

use app\common\controller\Api;

/**
 * 首页接口
 */
class Index extends Api
{

    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    /**
     * 首页
     * 
     */
    public function getWechatAuthUrl()
    {
        $url = \wechat\Wechat::getUrl();
//        $heder = "location:" . $url;
//        header($heder);
        return $url;
    }



    public function wechatAuth(){
        $code = $this->request->request('code');
        $state = $this->request->request('state');

        if(empty($code)) $this->error('授权失败');
        $result = \wechat\Wechat::getAccessToken($code,$state);

        if($result['code'] == 1){
            $this->success('',$result['data']);
        }else{
            $this->error($result['msg'],$result['data']);
        }


    }



    /************** test ***********/
    public function testpay(){
        $openid = 'oJWlT0f1GblQfzDTEdOkZxaQbx8k';
        $fee = 100.00;
        $details = '越南红心火龙果（小）';
        $order_sn = '20180605152818148488';
        $wxpay = new \wechat\WxPay();
        $data = $wxpay->pay($openid,$fee,$details,$order_sn);
        echo json_encode($data);
    }

    public function testwxlogin(){

        $user_info = [
            'openid' =>  "oJWlT0f1GblQfzDTEdOkZxaQbx8k",
            'nickname' => '嘻嘻哈哈',
            'headimgurl' =>  "http://thirdwx.qlogo.cn/mmopen/vi_32/sriadiaV6yAkjF5ia5Q4qibzYLyRD0zokzTwPxIgicdNSP0dvf9NfpNrwM6um2OibGQz3ZowXujqJYQStia8cicfa7OY1A/132"
        ];
        $data = \app\common\model\User::wxregister($user_info);
        if($data['code'] == 1){
            return ['code' => 1, 'msg' => $data['msg'], 'data' => $data['data']];

        }else{
            return ['code' => 0, 'msg' => $data['msg'], 'data' => $data['data']];

        }

    }

    public function testorderpay($order_sn){
        update_pay_status($order_sn);


    }

    public function testwxmsg(){
        $wechat = new \wechat\Wechat();
        $wechat->sendWxMsg(4,3);
    }

    public function testmsg(){
        $goods = \app\admin\model\Goods::get(3);
        \app\admin\model\Goods::sendWxMsg($goods);
        
    }





}
